﻿/*
 * (c) Stefan Bojarovski 2010
 * */
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASTBuilder
{
    class LabelNode : NodesCollection, ITreeNode, ILabelNode
    {
        private LexemeType lexType = LexemeType.LABELED_BLOCK;
        private String Label;

        private List<ITreeNode> labeledBlockComponents;

        public LabelNode(String someLabel)
        {
            Label = someLabel;
            labeledBlockComponents = new List<ITreeNode>();
        }
        public LabelNode(String someLabel, List<ITreeNode> someBlock)
        {
            lexType = LexemeType.LABELED_BLOCK;
            Label = someLabel;
            labeledBlockComponents = someBlock;
        }
        public void addStatement(ITreeNode statement)
        {
            labeledBlockComponents.Add(statement);
        }
        public LexemeType getLexemeType()
        {
            return lexType;
        }
        public List<ITreeNode> getBlock()
        {
            return labeledBlockComponents;
        }
        public String getLabel()
        {
            return Label;
        }
        public void printNode(String parentLevel, StreamWriter someStream)
        {
            someStream.WriteLine(parentLevel + " " + "Label: " + Label);
            foreach (ITreeNode statement in labeledBlockComponents)
            {
                statement.printNode(parentLevel + " ", someStream);
            }
        }
        public List<Command> evalNode(ref Direction next,
                                        ref Dictionary<String, Pair<IdentifierType, object>> Environment)
        {
            List<Command> result = new List<Command>();
            if (next != Direction.JUMP)
            {
                foreach (ITreeNode comp in labeledBlockComponents)
                {
                    List<Command> temp = comp.evalNode(ref next, ref Environment);
                    foreach (Command comm in temp)
                    {
                        result.Add(comm);
                    }
                }
                return result;
            }
            else
            {
                result.Add(new Command(CommandType.VOID, null, null));
                return result;
            }
        }
    }
}
